import moment from 'moment';
import {isString} from 'lodash';
export default {
    formatSecond: function (value) {
        var theTime = parseInt(value);// 秒
        var theTime1 = 0;// 分
        var theTime2 = 0;// 小时
        if (theTime > 60) {
            theTime1 = parseInt(theTime / 60);
            theTime = parseInt(theTime % 60);
            if (theTime1 > 60) {
                theTime2 = parseInt(theTime1 / 60);
                theTime1 = parseInt(theTime1 % 60);
            }
        }

        var result = "" + parseInt(theTime);//秒

        if (10 > theTime > 0) {
            result = "0" + parseInt(theTime);//秒
        } else {
            result = "" + parseInt(theTime);//秒
        }

        if (10 > theTime1 > 0) {
            result = "0" + parseInt(theTime1) + "分" + result;//分，不足两位数，首位补充0，
        } else {
            result = "" + parseInt(theTime1) + "分" + result;//分
        }
        if (theTime2 > 0) {
            result = "" + parseInt(theTime2) + "时" + result;//时
        }
        return result;
    },
    //转化性别
    formatRole: function (row, column) {
        return row.sex == '0' ? "男" : row.sex == '1' ? "女" : "未知";
    },
    //时间格式化  
    tableFormat: function (row, column=null) {
        if(isString(row)){
            return moment(row).format("YYYY-MM-DD HH:mm:ss");
        }
        var date = row[column.property];
        if (date == undefined) {
            return "";
        }
        return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
    dateFormat: function (unix, code=null,time=false) {
        if(!unix)return '';
        if(code == 'cn'){
            if(time){
                return moment.unix(unix).format("YYYY年MM月DD日 HH:mm:ss");
            }
            return moment.unix(unix).format("YYYY年MM月DD日");
        }
        if(time){
            return moment.unix(unix).format("YYYY-MM-DD");
        }
        return moment.unix(unix).format("YYYY-MM-DD HH:mm:ss");
    },
    //计算时间差
    formatTimes: (startTime, endTime,type='minute') => {
        let diff = endTime - startTime;
        let num = 0;
        switch(type){
            case 'day':
                num=Math.floor(diff / (24 * 3600 * 1000));
                break;
            case 'hour':
                let rh = diff % (24 * 3600 * 1000);
                num = Math.floor(rh / (3600 * 1000));
            case 'minute':
                let rm = rh % (3600 * 1000);
                num = Math.floor(rm / (60 * 1000));
            case 'second':
                let rs = rm % (60 * 1000);
                num = Math.round(rs / 1000);
            default :
                num=0;
                
        }
        return num>0?num:0;
    }
}